package com.qf.mybatis01.entity;

import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book implements Serializable {
    private Integer id;
    private String name;
    private String author;
    private String publish;
    private Integer sort;


    /**
     * 产生一本新的随机属性的book
     * @return
     */
    public static Book randomBook(){

        final Book book = new Book();

        // 产生随机的书名，借助hutool工具类
        final String name = RandomUtil.randomString(20);

        // 产生随机的作者名
        final String author = RandomUtil.randomString(10);

        // 随机出版社
        final String publish = RandomUtil.randomString(25);

        // 随机的排名
        final int sort = RandomUtil.randomInt(0, 1000000);

        book.setName(name);
        book.setAuthor(author);
        book.setPublish(publish);
        book.setSort(sort);

        return book;
    }


    /**
     * 产生多本随机的书
     * @param size
     * @return
     */
    public static List<Book> randomBooks(Integer size){

        final ArrayList<Book> books = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            books.add(randomBook());
        }

        return books;

    }

    /**
     * 将book对象转换成 csv 字符串
     * 0ko1r3qshjqxokjtaw3z,8p8lotxmk9,9p7gaosed35ova7phi3pn9udm,529366
     * @return
     */
    public String toCSVString(){
        return this.name+","+this.author+","+this.publish+","+this.sort;
    }

    public static Book csvToBook(String csv){
        String[] split = csv.split(",");
        Book book = new Book();
        book.setName(split[0]);
        book.setAuthor(split[1]);
        book.setPublish(split[2]);
        book.setSort(Integer.valueOf(split[3]));

        return book;
    }



}
